package unsafe;

import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author Panbo Guo
 * @date 2021/7/14
 */
public class CasLongArrayTest {
    private static Unsafe unsafe;

    static {
        try {
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
            theUnsafe.setAccessible(true);
            unsafe = (Unsafe) theUnsafe.get(null);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        long[] arr = new long[5];
        long offset = unsafe.arrayBaseOffset(arr.getClass());
        int scale = unsafe.arrayIndexScale(arr.getClass());

        unsafe.compareAndSwapLong(arr, (3 * scale) + offset, 0, 123L);
        System.out.println(Arrays.toString(arr));
    }
}
